package com.codedancer.campus.client.schedule;

import cn.hutool.core.util.ObjectUtil;
import com.codedancer.campus.common.enums.CommentNumberKeyEnum;
import com.codedancer.campus.common.enums.KudosNumberKeyEnum;
import com.codedancer.campus.common.enums.ViewNumberKeyEnum;
import com.codedancer.campus.common.utils.RedisUtil;
import com.codedancer.campus.mapper.ConfessionalWallMapper;
import com.codedancer.campus.po.ConfessionalWall;
import java.util.Set;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 表白墙定时任务
 */
@Component
@Slf4j
public class ConfessionalWallSchedule {

    @Autowired
    private ConfessionalWallMapper confessionalWallMapper;
    @Autowired
    private RedisUtil redisUtil;
    // 注入浏览记录redisTemplate
    @Resource(name = "viewRedisTemplate")
    private StringRedisTemplate viewRedisTemplate;
    // 注入评论记录redisTemplate
    @Resource(name = "commentRedisTemplate")
    private StringRedisTemplate commentRedisTemplate;
    // 点赞浏览记录redisTemplate
    @Resource(name = "kudosRedisTemplate")
    private StringRedisTemplate kudosRedisTemplate;

    /**
     * 更新浏览记录 每隔5分钟跑一次
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    private void updateViewNumber() {
        redisUtil.setRedisTemplate(viewRedisTemplate);
        log.info("开始更新浏览记录");
        long start = System.currentTimeMillis();
        // 获取失物招领有关的key
        Set<String> keys = redisUtil.getKeys(ViewNumberKeyEnum.CONFESSIONAL_WALL + "*");
        for (String key : keys) {
            Long viewNumber = redisUtil.size(key);
            String[] split = key.split(":");
            ConfessionalWall confessionalWall = confessionalWallMapper.selectById(split[1]);
            if (ObjectUtil.isNotEmpty(confessionalWall)) {
                confessionalWall
                    .setViewCount(confessionalWall.getViewCount() + viewNumber.intValue());
                confessionalWallMapper.updateById(confessionalWall);
                log.info("浏览表白墙uuid：{}",confessionalWall.getUuid());
            }
            // 删除key
            redisUtil.delete(key);
        }
        long end = System.currentTimeMillis();
        log.info("表白墙浏览量入库结束，耗时：{}", end - start);
    }


    /**
     * 更新表白墙评论记录 每隔10分钟跑一次
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    private void updateCommentNumber() {
        redisUtil.setRedisTemplate(commentRedisTemplate);
        log.info("开始更新表白墙评论记录");
        long start = System.currentTimeMillis();
        // 获取表白墙评论有关的key
        Set<String> keys = redisUtil.getKeys(CommentNumberKeyEnum.CONFESSIONAL_WALL_COMMENT + "*");
        for (String key : keys) {
            Long commentCount = redisUtil.getCountByIncOrdec(key);
            String[] split = key.split(":");
            ConfessionalWall confessionalWall = confessionalWallMapper.selectById(split[1]);
            if (ObjectUtil.isNotEmpty(confessionalWall)) {
                confessionalWall
                    .setCommentCount(commentCount.intValue() + confessionalWall.getCommentCount());
                confessionalWallMapper.updateById(confessionalWall);
                log.info("评论表白墙uuid：{}",confessionalWall.getUuid());
            }
            // 删除key
            redisUtil.delete(key);
        }
        long end = System.currentTimeMillis();
        log.info("表白墙评论量入库结束，耗时：{}", end - start);
    }

    /**
     * 更新表白墙点赞记录 每隔14分钟跑一次
     */
    @Scheduled(cron = "0 0/14 * * * ?")
    private void updateKudosNumber() {
        redisUtil.setRedisTemplate(kudosRedisTemplate);
        log.info("开始更新表白墙点赞记录");
        long start = System.currentTimeMillis();
        // 获取表白墙评论有关的key
        Set<String> keys = redisUtil.getKeys(KudosNumberKeyEnum.CONFESSIONAL_WALL_KUDOS + "*");
        for (String key : keys) {
            Long kudosCount = redisUtil.getCountByIncOrdec(key);
            String[] split = key.split(":");
            ConfessionalWall confessionalWall = confessionalWallMapper.selectById(split[1]);
            if (ObjectUtil.isNotEmpty(confessionalWall)) {
                confessionalWall
                    .setKudosCount(kudosCount.intValue() + confessionalWall.getKudosCount());
                confessionalWallMapper.updateById(confessionalWall);
                log.info("点赞表白墙uuid：{}",confessionalWall.getUuid());
            }
            // 删除key
            redisUtil.delete(key);
        }
        long end = System.currentTimeMillis();
        log.info("表白墙点赞量入库结束，耗时：{}", end - start);
    }
}
